package com.idata.model.utils;

import cn.hutool.core.collection.ListUtil;
import com.idata.model.dto.KGGraphExploreNodeDTO;
import com.idata.model.response.GraphExploreResponse;
import com.idata.model.response.GraphExploreResponse.Edge;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class GraphExploreCombineUtils {
    public static GraphExploreResponse<KGGraphExploreNodeDTO> combine(GraphExploreResponse<KGGraphExploreNodeDTO> ... responseList) {
        // 将两个GraphExploreResponse进行整合，有可能会出现node节点重复，所以转成set去重
        Set<KGGraphExploreNodeDTO> nodes = ListUtil.toList(responseList).stream().map(GraphExploreResponse::getNodes).flatMap(List::stream).collect(Collectors.toSet());

        List<Edge> edges = ListUtil.toList(responseList).stream().map(GraphExploreResponse::getEdges).flatMap(List::stream).collect(Collectors.toList());
        return new GraphExploreResponse<KGGraphExploreNodeDTO>().setEdges(edges).setNodes(new ArrayList<>(nodes));
    }
}
